package com.example.springbootrbac.Filter;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;

import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
public class SecurityTokenProcessFilter extends GenericFilterBean {
    @Resource(name = "userDetailsServiceImpl")
    private UserDetailsService userDetailsService;


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入安全框架拦截");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        // 获取请求头中的认证信息
        String token = request.getHeader("authentication");
        if (ObjectUtils.isNotEmpty(token)) {
            // 根据用户名获取用户权限信息
            System.out.println("进入安全框架if拦截");
            UserDetails userDetails = userDetailsService.loadUserByUsername(token);
            // 创建认证对象 Authentication，并存入 SecurityContext 表示认证成功
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails.getUsername(), userDetails.getPassword(), userDetails.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
        filterChain.doFilter(servletRequest, servletResponse);  //若被拦截，此处将会转入对应处理器
        System.out.println("退出安全框架拦截");
    }
}
